package org.bluweb.g4wicket.slideshow;

import java.util.HashMap;
import java.util.Map;

import org.apache.wicket.markup.html.IHeaderResponse;
import org.bluweb.g4wicket.G4WicketComponent;
import org.bluweb.g4wicket.G4WicketEvent;


@SuppressWarnings("serial")
public class SlideShow extends G4WicketComponent {

	private Map<String,String> options = new HashMap<String, String>();
	private String rssUrl;
	
	public SlideShow( String id, String rssUrl ) {
		super( id );
		this.rssUrl = rssUrl;
	}
	
	public SlideShow setLinkTarget( String target ) {
		return this.setOption( "linkTarget", target );
	}
	
	public SlideShow setDisplayTime( int time ) {
		return this.setOption( "displayTime", String.valueOf( time ) );
	}

	public SlideShow setTransistionTime( int time ) {
		return this.setOption( "transistionTime", String.valueOf( time ) );
	}
	
	private SlideShow setOption( String name, String value ) {
		this.options.put( name, value );
		return this;
	}

	@Override
	protected void onBeforeRender() {
		super.onBeforeRender();
		
		Map<String,Object> parameters = new HashMap<String,Object>();
		parameters.put( "url", this.rssUrl );
		
		StringBuilder builder = new StringBuilder();
		for( String name : options.keySet() ) {
			if( builder.length() > 0 ) {
				builder.append( ',' );
			}
			builder.append( name ).append( ":" );
			builder.append( options.get( name ) );
		}
		
		parameters.put( "options", builder.toString() );
		
		this.setJavaScriptTemplate( "SlideShow.js", true, parameters );
	}

	@Override
	public void renderHead(IHeaderResponse response) {
		super.renderHead( response );
		response.renderJavascriptReference( "http://www.google.com/uds/solutions/slideshow/gfslideshow.js" );
	}
	
	public void pause( boolean showPauseImage, G4WicketEvent event ) {

		this.execute( this.getId() + ".pause(" + showPauseImage + ")", event );
	}
	
	public void resume( G4WicketEvent event ) {

		this.execute( this.getId() + ".resume()", event );
	}
}
